package me.w70.fps.raycast;

import me.w70.fps.constants.Angles;
import me.w70.fps.render.GameWindow;

public class TrigTableBuilder {
	protected Angles angles;
	protected Raycaster raycaster;
	protected GameWindow renderer;
	int i;
	float radian;
	
	public float arcToRad(float arcAngle) { return (((float)(arcAngle * Math.PI)) / ((float)angles.ANGLE180)); }
	
	public TrigTableBuilder(Angles angle, Raycaster raycaster, GameWindow renderer) {
		this.angles = angle;
		this.raycaster = raycaster;
		this.renderer = renderer;
	}
	
	public void buildtrigtables() {
		for (i = 0; i <= angles.ANGLE360; i++) {
			radian = arcToRad(i) + (float)(0.0001);
			raycaster.sintable[i] = (float)Math.sin(radian);
			raycaster.inversesintable[i] = (1.0F / (raycaster.sintable[i]));
			raycaster.costable[i] = (float)Math.cos(radian);
			raycaster.inversecostable[i] = (1.0F / (raycaster.costable[i]));
			raycaster.tantable[i] = (float)Math.tan(radian);
			raycaster.inversetantable[i] = (1.0F / raycaster.tantable[i]);
			
			// Left
			if ((i >= angles.ANGLE90) && (i < angles.ANGLE270)) {
				raycaster.xsteptable[i] = (float)(raycaster.TILE_SIZE / raycaster.tantable[i]); 
	        
				if (raycaster.xsteptable[i] > 0) {
					raycaster.xsteptable[i] =- raycaster.xsteptable[i]; }
			}
			// Right
			else {
				raycaster.xsteptable[i] = (float)(raycaster.TILE_SIZE / raycaster.tantable[i]);
				if (raycaster.xsteptable[i] < 0) {
					raycaster.xsteptable[i] =- raycaster.xsteptable[i]; }
			}

			// Down
			if ((i >= angles.ANGLE0) && (i < angles.ANGLE180)) {
				raycaster.ysteptable[i] = (float)(raycaster.TILE_SIZE * raycaster.tantable[i]);
				if (raycaster.ysteptable[i] < 0) {
					raycaster.ysteptable[i] =- raycaster.ysteptable[i]; }
			}
			// Up
			else {
				raycaster.ysteptable[i] = (float)(raycaster.TILE_SIZE * raycaster.tantable[i]);
				if (raycaster.ysteptable[i] > 0) {
					raycaster.ysteptable[i] =- raycaster.ysteptable[i]; }
			}
	    }

	    for (i = -angles.ANGLE30; i <= angles.ANGLE30; i++) {
	        radian = arcToRad(i);
	        // No negative angles, go from 0 to 320 degrees.
	        renderer.fishbowleffecttable[i + angles.ANGLE30] = (float)(1.0F / Math.cos(radian));
	    }
	}
}
